package controllers

import (
	"fmt"
	"goend/logs"
	"io"
	"os"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/robfig/cron"
)

var f *os.File

func init() {
	gin.DisableConsoleColor()
	//ginlog
	nowdate := time.Now()
	year, month, day := nowdate.Date()
	logFileName := fmt.Sprintf("logs/gin%d%d%d.log", year, int(month), day)
	_, err := os.Stat(logFileName)
	if os.IsNotExist(err) {
		f, _ = os.Create(logFileName)
		logs.PrintLog("cotroller: " + logFileName + "is not exist create")
	} else {
		f, _ = os.OpenFile(logFileName, os.O_APPEND|os.O_WRONLY, 0644)
		logs.PrintLog("cotroller: " + logFileName + "exist create,bind")
	}
	gin.DefaultWriter = io.MultiWriter(f)
	//定时任务
	cron.New().AddFunc("* * 0 * * *", func() {
		nowdate := time.Now()
		year, month, day := nowdate.Date()
		logFileName := fmt.Sprintf("logs/gin%d%d%d.log", year, int(month), day)
		_, err = os.Stat(logFileName)
		if os.IsNotExist(err) {
			f, _ = os.Create(logFileName)
			logs.PrintLog("cotroller: " + logFileName + "is not exist create")
		} else {
			f, _ = os.OpenFile(logFileName, os.O_APPEND|os.O_WRONLY, 0644)
			logs.PrintLog("cotroller: " + logFileName + "exist create,bind")
		}
		gin.DefaultWriter = io.MultiWriter(f)
	})
}
